package com.processmining.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.processmining.entity.Algo;
import com.processmining.service.IAlgoService;
import com.processmining.util.Config;
import com.processmining.util.ConfigParser;
import com.processmining.util.response.Code;
import com.processmining.util.response.ResponseAlgoDelete;
import com.processmining.util.response.ResponseData;
import com.processmining.util.response.ResponseWithConfig;
import com.processmining.util.response.ResponseWithPayload;

@Controller
public class AlgoController {

	@Autowired
	private IAlgoService algoService;

	@ResponseBody
	@RequestMapping("/manager/uploadAlgo")
	public ResponseWithPayload uploadJarAndConfig(@RequestParam(value="package") MultipartFile pack, @RequestParam(value="config") MultipartFile conf,
			@RequestParam(value="algoName") String name, @RequestParam(value="description") String desc,@RequestParam(value="type")Integer type, HttpSession session)
			throws IllegalStateException, IOException {
		String prefix = session.getServletContext().getRealPath("/jars");
		String packFileName = UUID.randomUUID().toString();
		String confFileName = UUID.randomUUID().toString();
		File packFile = null;
		File confFile = null;
		// try {
		packFile = new File(prefix, packFileName);
		confFile = new File(prefix, confFileName);
		pack.transferTo(packFile);
		conf.transferTo(confFile);
		// } catch (Exception e) {
		// return new ResponseData(Code.SYSTEM_ERROR);
		// }
		Algo algo = new Algo();
		System.out.println(prefix + confFileName);
		Config config = ConfigParser.parseConfig(prefix + "/" + confFileName);
		algo.setClassname(config.getClassName());
		algo.setMethod(config.getMethodName());
		algo.setPackagename(config.getPackageName());
		algo.setInfo(desc);
		algo.setPath(prefix + "/" + packFileName);
		algo.setConfigpath(prefix + "/" + confFileName);
		algo.setName(name);
		algo.setType(type);
		int status = algoService.createAlgo(algo);
		List<Algo> alg=null;
		switch (type) {
		case 0:
			alg = algoService.listMergeAlgo();
			break;

		case 1:
			alg = algoService.listMiningAlgo();
			break;
			
		default:
			alg = algoService.getAllAlgo();
			break;
			
		}

		// System.out.print(status);
		return new ResponseWithPayload(status,alg);
	}

	@ResponseBody
	@RequestMapping("/manager/deleteJarAndConfig/{aid}")
	public ResponseAlgoDelete deleteJarAndConfig(@PathVariable Integer aid) {
		Algo algo = algoService.getAlgoById(aid);
		if (algo == null) {
			return new ResponseAlgoDelete(Code.SYSTEM_ERROR,null);
		}
		int type=algo.getType();
		String jarPath = algo.getPath();
		String configPath = algo.getConfigpath();
		try {
			File jarFile = new File(jarPath);
			File config = new File(configPath);
			if (jarFile.exists()) {
				jarFile.delete();
			}
			if (config.exists()) {
				config.delete();
			}
		} catch (Exception e) {
			return new ResponseAlgoDelete(Code.SYSTEM_ERROR,null);
		}
		int status=algoService.deleteAlgoById(algo.getId());
		List<Algo> nextAlgo=null;
		if(type==0){
			nextAlgo=algoService.listMergeAlgo();
		}
		if(type==1){
			nextAlgo=algoService.listMiningAlgo();
		}
		return new ResponseAlgoDelete(status,nextAlgo);
	}
	
	@ResponseBody
	@RequestMapping(value="/algoConfig/{AlgoId}",method=RequestMethod.GET)
	public ResponseData getAlgoParamList(@PathVariable(value="AlgoId")Integer algoId){
		
		ResponseWithConfig response = new ResponseWithConfig();
		
		Config config = algoService.getAlgoConfig(algoId);
		response.setCode(Code.SUCCEED);
		response.setConfig(config);
		
		return response;
		
	}
	
	@ResponseBody
	@RequestMapping(value="/algo/{AlgoId}",method=RequestMethod.GET)
	public Algo getAlgoById(@PathVariable(value="AlgoId")Integer algoId){
		Algo algo=algoService.getAlgoById(algoId);
		return algo;
	}
	
	
	
	
	
	
}
